package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db1.*;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.*;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.CommonUtils;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RoadmapParamServiceImpl implements RoadmapParamService {

    @Autowired
    private RoadmapBigTypeDao roadmapBigTypeDao;

    @Autowired
    private RoadmapSmallTypeDao roadmapSmallTypeDao;

    @Autowired
    private RoadmapParamDao roadmapParamDao;

    @Autowired
    private TechDeviceParamDao techDeviceParamDao;

    @Autowired
    private PcbaDeviceTypeDao pcbaDeviceTypeDao;

    @Autowired
    private TechDeviceParamTypeDao techDeviceParamTypeDao;

    @Autowired
    private TechCapacityRuleDao techCapacityRuleDao;

    @Autowired
    private LineTechBigTypeDao lineTechBigTypeDao;

    @Autowired
    private LineTechSmallTypeDao lineTechSmallTypeDao;

    @Autowired
    private RoadmapH3cDao roadmapH3cDao;

    @Autowired
    private CommonJudgeCanDeleteService commonService;

    @Value("${personal.config.amazon-s3.url}")
    private String url;
    @Value("${personal.config.amazon-s3.file-bucket}")
    private String fileBucket;
    @Value("${personal.config.env}")
    private String env;

    @Qualifier("MysqlTransactionManager1")
    @Autowired
    private DataSourceTransactionManager transactionManager1;

    @Autowired
    private OperateService operateService;

    private String operateSmallType = "Roadmap 能力项";

    @Override
    public List<RoadmapParam> getAllRoadmapParam(String searchValue, Integer roadmapBigTypeId, Integer roadmapSmallTypeId) {
        List<RoadmapParam> list = roadmapParamDao.getAllRoadmapParam(searchValue, roadmapBigTypeId, roadmapSmallTypeId);
        for (RoadmapParam roadmapParam : list) {

            if (roadmapParam.getRoadmapParamPicture() != null && !"".equals(roadmapParam.getRoadmapParamPicture())) {
                if (env != null && env.contains("pro"))  {
                    roadmapParam.setRoadmapParamPictureBase64(CommonUtils.base64Prefix() + CommonUtils.getImageBase64FromUrl(url + "/" + fileBucket + "/" + roadmapParam.getRoadmapParamPicture()));
                } else {
                    roadmapParam.setRoadmapParamPictureBase64(CommonUtils.base64Prefix() + CommonUtils.getImageBase64FromUrl(CommonUtils.returnHostPrefixUrl() + roadmapParam.getRoadmapParamPicture()));
                }
            }

            if (roadmapParam.getTechDeviceParamKeys() != null && !"".equals(roadmapParam.getTechDeviceParamKeys())) {
                String[] keysArr = roadmapParam.getTechDeviceParamKeys().split(",");
                Integer keyFromType = roadmapParam.getKeyFromType();
                if (keyFromType != null && keyFromType == 1) {
                    List<TechCapacityRule> list1 = new ArrayList<>();
                    for (String s : keysArr) {
                        TechCapacityRule techCapacityRule = techCapacityRuleDao.getTechCapacityRuleByKey(s);
                        if (techCapacityRule != null) {
                            LineTechBigType byId = lineTechBigTypeDao.getLineTechBigTypeById(techCapacityRule.getLineTechBigTypeId());
                            if (byId != null) {
                                techCapacityRule.setLineTechBigType(byId);
                            }
                            LineTechSmallType lineTechSmallType = lineTechSmallTypeDao.getLineTechSmallTypeById(techCapacityRule.getLineTechSmallTypeId());
                            if (lineTechSmallType != null) {
                                techCapacityRule.setLineTechSmallType(lineTechSmallType);
                            }
                            list1.add(techCapacityRule);
                        }
                    }
                    roadmapParam.setTechCapacityRuleList(list1);
                } else {
                    List<TechDeviceParam> list1 = new ArrayList<>();
                    for (String s : keysArr) {
                        TechDeviceParam techDeviceParamByKey = techDeviceParamDao.getTechDeviceParamByKey(s);
                        if (techDeviceParamByKey != null) {
                            PcbaDeviceType byId = pcbaDeviceTypeDao.getById(techDeviceParamByKey.getPcbaDeviceTypeId());
                            if (byId != null) {
                                techDeviceParamByKey.setPcbaDeviceType(byId);
                            }
                            TechDeviceParamType pcbaDeviceParamTypeById = techDeviceParamTypeDao.getPcbaDeviceParamTypeById(techDeviceParamByKey.getTechDeviceParamTypeId());
                            if (pcbaDeviceParamTypeById != null) {
                                techDeviceParamByKey.setTechDeviceParamType(pcbaDeviceParamTypeById);
                            }
                            list1.add(techDeviceParamByKey);
                        }
                    }
                    roadmapParam.setTechDeviceParamList(list1);
                }
            }
            roadmapParam.setRoadmapBigType(roadmapBigTypeDao.getRoadmapBigTypeById(roadmapParam.getRoadmapBigTypeId()));
            roadmapParam.setRoadmapSmallType(roadmapSmallTypeDao.getRoadmapSmallTypeById(roadmapParam.getRoadmapSmallTypeId()));
        }
        return list;
    }

    @Override
    public Result deleteRoadmapParam(Integer roadmapParamId) {
        try {
            if (roadmapParamId == null || "".equals(roadmapParamId.toString())) {
                return ResultUtil.error(500, "缺少参数roadmapParamId", "Miss Param roadmapParamId", null);
            }
            /*Map<String, Integer> stringIntegerMap = commonService.judgeCanDeleteRoadmapBigType(roadmapBigTypeId);
            if(stringIntegerMap.get("roadmapSmallTypeNum") > 0 || stringIntegerMap.get("roadmapParamNum") > 0){
                return ResultUtil.error(500,
                        "禁止删除，当前项" + stringIntegerMap.get("roadmapSmallTypeNum") + "处关联小类，"
                                + stringIntegerMap.get("roadmapParamNum") + "处关联roadmap能力项" ,
                        "Forbidden Delete", null);
            }*/

            String userId = UserHeaderHolder.getUserId();
            RoadmapParam oldRoadmapParam = roadmapParamDao.getRoadmapParamById(roadmapParamId);

            Integer integer = roadmapParamDao.deleteRoadmapParam(roadmapParamId);
            if (integer > 0) {

                // 操作日志
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 2, 1, new Date(), JSONObject.toJSONString(oldRoadmapParam), null));

                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result addOrUpdateRoadmapParam(String mode, RoadmapParam roadmapParam) {
        try {
            if (roadmapParam == null) {
                return ResultUtil.error(500, "缺少参数roadmapParam", "Miss Param roadmapParam", null);
            }
            if (mode == null || "".equals(mode)) {
                return ResultUtil.error(500, "缺少参数mode", "Miss Param mode", null);
            }
            if (!("edit".equals(mode) || "add".equals(mode))) {
                return ResultUtil.error(500, "mode 参数错误", "mode Param Error", null);
            }

            String userId = UserHeaderHolder.getUserId();

            if ("edit".equals(mode)) {
                if (roadmapParam.getRoadmapParamId() == null || "".equals(roadmapParam.getRoadmapParamId().toString())) {
                    return ResultUtil.error(500, "缺少参数roadmapParamId", "Miss Param roadmapParamId", null);
                }

                RoadmapParam oldRoadmapParam = roadmapParamDao.getRoadmapParamById(roadmapParam.getRoadmapParamId());
                roadmapParam.setUpdateTime(new Date());
                Integer integer = roadmapParamDao.updateRoadmapParam(roadmapParam);
                if (integer > 0) {

                    // 操作日志
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, new Date(), JSONObject.toJSONString(oldRoadmapParam), JSONObject.toJSONString(roadmapParam)));

                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            } else {
                roadmapParam.setCreateTime(new Date());
                Integer integer = roadmapParamDao.addRoadmapParam(roadmapParam);
                if (integer > 0) {

                    // 操作日志
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 1, 1, new Date(), null, JSONObject.toJSONString(roadmapParam)));

                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result updateRoadmapRule(Integer roadmapParamId, Integer valueAlgorithmType, String techDeviceParamKeys, Integer ruleAlgorithmType, Integer keyFromType) {
        try {
            if (roadmapParamId == null || "".equals(roadmapParamId.toString())) {
                return ResultUtil.error(500, "缺少参数roadmapParamId", "Miss Param roadmapParamId", null);
            }
            /*if (valueAlgorithmType == null || "".equals(valueAlgorithmType.toString())) {
                return ResultUtil.error(500, "缺少参数valueAlgorithmType", "Miss Param valueAlgorithmType", null);
            }
            if (techDeviceParamKeys == null || "".equals(techDeviceParamKeys)) {
                return ResultUtil.error(500, "缺少参数techDeviceParamKeys", "Miss Param techDeviceParamKeys", null);
            }*/
            if (ruleAlgorithmType == null || "".equals(ruleAlgorithmType.toString())) {
                return ResultUtil.error(500, "缺少参数ruleAlgorithmType", "Miss Param ruleAlgorithmType", null);
            }
            /*if (keyFromType == null || "".equals(keyFromType.toString())) {
                return ResultUtil.error(500, "缺少参数keyFromType", "Miss Param keyFromType", null);
            }*/

            String userId = UserHeaderHolder.getUserId();
            RoadmapParam oldRoadmapParam = roadmapParamDao.getRoadmapParamById(roadmapParamId);

            Integer integer = roadmapParamDao.updateRoadmapRule(roadmapParamId, valueAlgorithmType, techDeviceParamKeys, ruleAlgorithmType, keyFromType);
            if (integer > 0) {

                RoadmapParam newRoadmapParam = new RoadmapParam();
                newRoadmapParam.setRoadmapParamId(roadmapParamId);
                newRoadmapParam.setValueAlgorithmType(valueAlgorithmType);
                newRoadmapParam.setTechDeviceParamKeys(techDeviceParamKeys);
                newRoadmapParam.setRuleAlgorithmType(ruleAlgorithmType);
                newRoadmapParam.setKeyFromType(keyFromType);
                // 操作日志
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, new Date(), JSONObject.toJSONString(oldRoadmapParam), JSONObject.toJSONString(newRoadmapParam)));

                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result clearRoadmapParamRule(Integer roadmapParamId) {
        try {
            if (roadmapParamId == null || "".equals(roadmapParamId.toString())) {
                return ResultUtil.error(500, "缺少参数roadmapParamId", "Miss Param roadmapParamId", null);
            }

            String userId = UserHeaderHolder.getUserId();
            RoadmapParam oldRoadmapParam = roadmapParamDao.getRoadmapParamById(roadmapParamId);

            Integer integer = roadmapParamDao.updateRoadmapRule(roadmapParamId, null, null, null, null);
            if (integer > 0) {

                // 操作日志
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, new Date(), JSONObject.toJSONString(oldRoadmapParam), null));

                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
